//
//  VCMineSetting.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/26.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCMineSetting: VCBase {
    
    // 头部背景
    private lazy var topBackground: UIView = {
        let ui = UIView()
        ui.backgroundColor = COLOR_MINE_TOP
        ui.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: realHeight)
        return ui
    }()
    
    // 头部布局
    private lazy var mineSetHeaderView: MineSettingView = {
        let ui = MineSettingView()
        ui.frame = CGRect(x: 0, y: realHeight, width: SCREEN_WIDTH, height: 170)
        return ui
    }()
    
    // 躯体布局
    private lazy var mineSettingBodyView: MineSettingBodyView = {
        let uiY = mineSetHeaderView.frame.maxY + MARGIN20
        let ui = MineSettingBodyView(frame: CGRect(x: MARGIN, y: uiY, width: SCREEN_WIDTH - MARGIN * 2, height: 360))
        ui.backgroundColor = UIColor.white
        ui.layer.cornerRadius = 8
        ui.layer.masksToBounds = true
        return ui
    }()
    
    private lazy var cleanView:CleanView = {
        let ui = CleanView()
        ui.delegate = self
        ui.frame = view.frame
        return ui
    }()
    
    // scrollview
    private lazy var scrollView: UIScrollView = {
        let ui = UIScrollView()
        ui.frame = CGRect(x: 0, y: realHeight, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - realHeight)
        ui.backgroundColor = COLOR_MINE_TOP
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_views()
        init_event()
        init_listener()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        init_data()
    }
}

// MARK: 逻辑部分
extension VCMineSetting {
    
    private func init_listener() -> Void {
        
        // 头部
        mineSetHeaderView.complete = {item in
            switch item {
            case 0:
                self.pushVc(title: "系统公告", vc: VCSystemTip())
            case 1:
                self.pushVc(title: "个人资料", vc: VCTopInfomation())
            case 2:
                self.pushVc(title: "报销凭证", vc: VCExpense())
            case 3:
                self.pushVc(title: "开通会员", vc: VCOpenMember())
            default:
                break
            }
        }
        
        // 躯体
        mineSettingBodyView.complete = {item in
            switch item {
            case 0:
                self.pushVc(title: "常用信息", vc: VCBodyInfomation())
            case 1:
                self.pushVc(title: "修改登录密码", vc: VCPwdmanger())
            case 2:
                self.pushVc(title: "登录设置", vc: VCLoginManger())
            case 3:
                self.pushVc(title: "支付设置", vc: VCPayManger())
            case 4:
                self.clearData()
            default:
                break
            }
        }
    }
    
    private func clearData() -> Void {
        tabBarController?.tabBar.isHidden = true
        view.addSubview(cleanView)
    }
}

// MARK: 清理缓存
extension VCMineSetting:CleanViewDelegate {
    
    func clear_remove() {
        removeClearView()
    }
    
    func clear_finish() {
        removeClearView()
        // 切到登录界面
        self.show(text: "清除成功")
        UserDefaults.standard.removeObject(forKey: SP_PASSWORD)
        NotificationCenter.default.post(name: CUSTOM_SWITCH_NOTIFICATIONCENTER, object: "exit")
    }
    
    func removeClearView() -> Void {
        tabBarController?.tabBar.isHidden = false
        cleanView.removeFromSuperview()
    }
}

// MARK: 初始化
extension VCMineSetting {
    
    private func init_event() -> Void {
        scrollView.showsVerticalScrollIndicator = false
        scrollView.contentInset = UIEdgeInsets(top: -realHeight, left: 0, bottom: 0, right: 0)
        scrollView.contentSize = CGSize(width: SCREEN_WIDTH, height: mineSettingBodyView.frame.maxY + MARGIN100)
    }
    
    private func init_data() -> Void {
        mineSetHeaderView.resetUserName = UserDefaults.standard.value(forKey: SP_USERNAME) as? String
        mineSetHeaderView.resetPreTime = UserDefaults.standard.value(forKey: SP_UPDATETIME) as? String
        mineSetHeaderView.resetMemberIndex = UserDefaults.standard.value(forKey: SP_PERMISSIONID) as? Int
    }
    
    private func init_views() -> Void {
        view.addSubview(topBackground)
        scrollView.addSubview(mineSetHeaderView)
        scrollView.addSubview(mineSettingBodyView)
        view.addSubview(scrollView)
    }
}
